package com.example.studentmanagersystem;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;
import android.widget.Toast;

import com.fs.po.Stu;
import com.fs.po.Tec;
import com.fs.utils.RegistUtil;

/**
 * User regist, Admin,Teacher,Student
 * @author chenwenfei
 *
 */
public class RegistActivity extends Activity {
	
	private String USER;
	private CheckBox limitCheckBox;
	private EditText nameRegistET,passwordRegistET,configPasswordET,phoneET,ageET;
	private RadioGroup sexRadioGroup,userRadioGroup;
	private Button registSubmitBtn,actionBack,actionMenu;
	private TextView sexResult;
	private Stu stu;
	private Tec tec;
	RegistUtil registUtil=new RegistUtil(this);
	
	public String TYPE;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.activity_main_regist);
		this.init();
		this.eventClickAction();
	}
	
	/**
	 * initialize widget
	 */
	private void init() {
		
		this.userRadioGroup=(RadioGroup) this.findViewById(R.id.regist_activity_userSelectRadioGroup);
		this.limitCheckBox=(CheckBox) this.findViewById(R.id.regist_activity_CheckBox_agree);
		this.nameRegistET=(EditText) this.findViewById(R.id.regist_activity_inputregist_username);
		this.passwordRegistET=(EditText) this.findViewById(R.id.regist_activity_inputregist_password);
		this.configPasswordET=(EditText) this.findViewById(R.id.regist_activity_input_configpassword);
		this.phoneET=(EditText) this.findViewById(R.id.regist_activity_inputregist_phonenumber);
		this.sexRadioGroup=(RadioGroup) this.findViewById(R.id.regist_activity_radioGroup);
		this.registSubmitBtn=(Button) this.findViewById(R.id.regist_activity_registsubmit_button);
		this.sexResult=(TextView) this.findViewById(R.id.regist_activity_SexResult);
		this.ageET=(EditText) this.findViewById(R.id.regist_activity_inputregist_age);
		
		this.actionBack=(Button) this.findViewById(R.id.regist_action_backButton);
		
		//regist button,Listener
		this.registSubmitBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				registUser();
			}
		});
		
		//ActionBar back Button
		this.actionBack.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
			
				Intent intent=new Intent(RegistActivity.this, MainActivity.class);
				RegistActivity.this.startActivity(intent);
				
			}
		});
	}
	
	/**
	 * Event Listener
	 */
	private void eventClickAction() {
		//Select regist user
		this.userRadioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				if(checkedId==R.id.regist_activity_RadioButton_stu){
					USER="STUDENT";
					TYPE="stu_";
				}else if(checkedId==R.id.regist_activity_RadioButton_teacher){
					USER="TEACHER";
					TYPE="tec_";
				}
			}
		});
		
		//Select Sex
		this.sexRadioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				if(checkedId==R.id.regist_activity_selectsex_man){
					sexResult.setText("man");
				}else if(checkedId==R.id.regist_activity_selectsex_women){
					sexResult.setText("woman");
				}
			}
		});
	}
	
	/**
	 * get user regist info
	 */
	private boolean getUserRegistInfo() {
		
		boolean isRegist=false;
		
		boolean name=TextUtils.isEmpty(nameRegistET.getText());
		boolean psw=TextUtils.isEmpty(passwordRegistET.getText());
		boolean cpsw=TextUtils.isEmpty(configPasswordET.getText());
		boolean phone=TextUtils.isEmpty(phoneET.getText());
		boolean sex=TextUtils.isEmpty(sexResult.getText());
		boolean age=TextUtils.isEmpty(ageET.getText());
		
		if(!(name&&psw&&cpsw&&phone&&sex&&age)){
		
			String registName=this.nameRegistET.getText().toString().trim();
			String registPassword=this.passwordRegistET.getText().toString().trim();
			String configPassword=this.configPasswordET.getText().toString().trim();
			String phoneNumber=this.phoneET.getText().toString().trim();
			String userSex=this.sexResult.getText().toString().trim();
			int userAge=Integer.parseInt(this.ageET.getText().toString().trim());
			stu=new Stu(registName, registPassword, phoneNumber, userSex, userAge);
			
			isRegist=true;
		}else {
			isRegist=false;
		}
		
		return isRegist;
		
	}
	
	/**
	 * Regist
	 */
	private void registUser() {
		
		 String NAME=TYPE+"registName";
		 String PASSWORD=TYPE+"registPsw";
		 String PHONE=TYPE+"registPhone";
		 String SEX=TYPE+"registSex";
		 String AGE=TYPE+"registAge";
		 
		boolean isTrue=this.getUserRegistInfo();
		if(("STUDENT".equals(USER))&&(isTrue)){
			
			//student regist
			this.registUtil.userRegist(NAME, PASSWORD, PHONE, SEX, AGE, stu, URL.STU_URL,TYPE);
			
			
		}else if(("TEACHER".equals(USER))&&(isTrue)){
			
			//teacher regist
			this.registUtil.userRegist(NAME, PASSWORD, PHONE, SEX, AGE, stu, URL.TEC_URL,TYPE);
			
		}
		
	}
	
}











